//
//  KKMainViewController.swift
//  SinaWeiBo
//
//  Created by King.Kong. on 16/5/19.
//  Copyright © 2016年 King.Kong. All rights reserved.
//

import UIKit

class KKMainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
       addChildViewControllers()
        
        let mainTabBar = KKMainTabBar()
        
//        self.tabBar = mainTabBar
        setValue(mainTabBar, forKey: "tabBar")
        
        // 给按钮添加监听事件
        mainTabBar.plusBtn.addTarget(self, action: #selector(KKMainViewController.plusBtnDidClick), forControlEvents: .TouchUpInside)
    }

    
    @objc private func plusBtnDidClick() {
        
        print("被点了")
        //添加撰写的选择视图
        let composeView = KKComposeView()
        
         composeView.show(targetVC: self)
//        let window = UIApplication.sharedApplication().keyWindow
//        window?.addSubview(composeView)

    }
   
    private func addChildViewControllers() {
        
        addChildViewController(KKHomeTableViewController(), title: "首页", imageName: "tabbar_home",index: 0)
         addChildViewController(KKMessageTableViewController(), title: "消息", imageName: "tabbar_message_center",index: 1)
         addChildViewController(KKDiscoveryTableViewController(), title: "发现", imageName: "tabbar_discover",index: 2)
         addChildViewController(KKProfileTableViewController(), title: "我的", imageName: "tabbar_profile",index: 3)
        
    }
    
    
    private func addChildViewController(vc:UIViewController, title:String, imageName:String,index:Int) {
        
//        vc.tabBarItem.title = title
//        vc.navigationItem.title = title
        // 替换成自定义的tabBarItem
        vc.tabBarItem = KKTabbarItem()
        
        //设置tag
        vc.tabBarItem.tag = index

        
        // 若标签和导航条的title一致可简写
        vc.title = title
        
        vc.tabBarItem.image = UIImage(named: imageName)
        
        // 设置选中图片  渲染之后才能展示出来
        vc.tabBarItem.selectedImage = UIImage(named: imageName + "_selected")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
        
        // 设置标签文字颜色
        vc.tabBarItem .setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.orangeColor()], forState: .Selected)
        
        // 统一设置颜色
//        tabBar.tintColor = UIColor.orangeColor()
        
        // 设置标签文字位置
        vc.tabBarItem.titlePositionAdjustment = UIOffset(horizontal: 0, vertical: -3)
        // 设置标签图片位置
        //        vc.tabBarItem.imageInsets = UIEdgeInsets(top: 5, left: 0, bottom: -5, right: 0)

        let nav = KKBaseNavController(rootViewController:vc)
        
        addChildViewController(nav)
        
    }

    //实现协议方法
    override func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem) {
        print(item.tag)
        //遍历'tabBar'子视图
        var index = 0
        for v in tabBar.subviews {
            if v.isKindOfClass(NSClassFromString("UITabBarButton")!) {
                //确定点击的是哪一个tabbarButton
                if index == item.tag{
                    //再遍历子视图 获取背景视图
                    for subView in v.subviews {
                        if subView.isKindOfClass(NSClassFromString("UITabBarSwappableImageView")!) {
                            subView.transform = CGAffineTransformMakeScale(0.5, 0.5)
                            //在动画闭包中 恢复到初始值
                            UIView.animateWithDuration(0.6, delay: 0, usingSpringWithDamping: 0.3, initialSpringVelocity: 4, options: [], animations: {
                                subView.transform = CGAffineTransformIdentity
                                }, completion: { (_) in
                                    print("👌")
                            })
                        }
                    }
                }
                index+=1
            }
        }
    }
    
}

    

